home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1994 December
/
PSL Monthly Shareware CD-ROM (Public Software Library)(December 1994).bin
/
prgmming
/
dos
/
c2
/
cgauss.hpp
< prev
next >
Wrap
C/C++ Source or Header
|
1992-06-25
|
3KB
|
79 lines
//---------------------------------------------------------------
// cgauss.hpp, definition of the cGAUSSIAN class
// ---------------------------------------------
//
// This class exists to produce streams of random
// variables whose distribution is Gaussian.
//
// To construct an instance, declare a variable
// with its mean and variance...
//
// cGAUSSIAN G( 0.0, 1.0 ); // unit normal distribution
//
// This instance can, and probably should, be a single
// global variable servicing all calls with the same
// mean/variance.
//
// Once the variable has been set up, individual random
// numbers can be obtained using the "next" method...
//
// double x = G.next(); // "x" gets a random number
//
// To gang-load an array of numbers, use the "vector"
// method...
//
// double v[ 100 ];
// G.vector( 0, 1, v, 100 ); // 100 elements of v
// // are filled with random
// // numbers whose mean is 0
// // and whose variance is 1
//
// NOTE: This file assumes that the application has
// included the following files...
// stdlib.h, for rand, RAND_MAX
// math.h, for cos, sqrt
// values.h for MINDOUBLE
//
//
// HISTORY: 6/92 Developed (GJV)
//--------------------------------------------------------------
//
//
//
//
//--------------------------------------------------------------
#ifndef RAND_MAX
#error CGAUSS.HPP: stdlib, math, and values must be included...
#endif
class cGAUSSIAN {
private:
double mean;
double variance;
protected:
public:
cGAUSSIAN( double _mean, double _variance ) {
mean = _mean;
variance = _variance;
}
double next( void ) {
double y;
do y = ( double )( rand() % RAND_MAX ) / ( double )RAND_MAX;
while( y < MINDOUBLE * 1e11 );
double z = ( double )( rand( ) % RAND_MAX ) /( double )RAND_MAX;
return( sqrt( -2.0 * log( y ) ) *
cos( 6.2831853072 * z ) *
sqrt( variance ) + mean );
}
void vector( double *v, int n ) {
while( n-- > 0 ) {
v[ n ] = next();
}
}
};
//--------------------------------------------------------------
//
//
//
//
//--------------------------------------------------------------